package aad.storyboard.transitions;

import java.util.ArrayList;

import aad.storyboard.controller.GUIComposer;
import aad.storyboard.controller.SceneTransition;
import aad.storyboard.model.Image;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;

public class FadeWithColorTransition extends SceneTransition{
	
      
   private FadInFadeOutHandler mHandler=new FadInFadeOutHandler();   
   private static Runnable mAnimationTask;
   
   private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   private ColorMatrix mCM = new ColorMatrix();  
   private ArrayList <Image> images;
   private float mAngle;
   private Canvas canvas;  
   
	public FadeWithColorTransition(int start, long duration) {
		super(start, duration);				
	}
	
	 private static void setContrastScaleOnly(ColorMatrix cm, float contrast) {
         float scale = contrast + 1.f;               
            	   cm.set(new float[] {
                scale, 0, 0, 0, 0,
                0, scale, 0, 0, 0,
                0, 0, scale, 0, 0,
                0, 0, 0, 1, 0 });
     }
     
     private static void setContrastScaleRed(ColorMatrix cm, float contrast) {
         float scale = contrast + 1.f;               
            	   cm.set(new float[] {
                scale, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, 0, 1, 0 });
     }
     
     private static void setContrastScaleGreen(ColorMatrix cm, float contrast) {
         float scale = contrast + 1.f;               
            	   cm.set(new float[] {
                0, 0, 0, 0, 0,
                0, scale, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, 0, 1, 0 });
     }
     
     private static void setContrastScaleYellow(ColorMatrix cm, float contrast) {
         float scale = contrast + 1.f;               
            	   cm.set(new float[] {
                scale, 0, 0, 0, 0,
                0, scale, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, 0, 1, 0 });
     }
     
     private static void setContrastScaleBlue(ColorMatrix cm, float contrast) {
         float scale = contrast + 1.f;               
            	   cm.set(new float[] {
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0,
                0, 0, scale, 0, 0,
                0, 0, 0, 1, 0 });
     }

     private static void setContrastScaleBlack(ColorMatrix cm, float contrast) {
     		 float scale = contrast + 1.f; 
            	 cm.set(new float[] {
                     scale, 0, 0, 0, 0, 
                     0, scale, 0, 0, 0,
                     0, 0, 0, scale, 0, 
                     0, 0, 0, 1, 0});
     }
     
	@Override
	public boolean execute() {
		
		 mAnimationTask = new Runnable()  
	     {  
	      
	       @Override
	       public void run()
	       {  
	    	   while(mAngle<255)
	    	   {
	    		   Paint paint = mPaint;
	            
	           ColorMatrix cm = new ColorMatrix();

	            mAngle += 2;
	            //mAngle-=2;
	            
	            //convert our animated angle [-180...180] to a contrast value of [-1..1]
	            float contrast = mAngle / 180.f;

	            setContrastScaleRed(cm, contrast);
	            paint.setColorFilter(new ColorMatrixColorFilter(cm));
	            
	            /*images=GUIComposer.getInstance().getGraphicalView().getImages();
	            for(Image m: images){
	            m.setPaint(paint);	            
	            }*/
	            GUIComposer.getInstance().getGraphicalView().setBackgroundColor(paint.getColor());
	            System.out.println("Setting the paint"+mAngle);
	            
	        	mHandler.sendEmptyMessage(0);
	        	mHandler.postDelayed(mAnimationTask, 1000);
	       }
	    	  
	       }
	      };
	     
	    mHandler.postDelayed(mAnimationTask, 1000);
		
		return false;
	}
	
	class FadInFadeOutHandler extends Handler  
    {  
     @Override
	public void handleMessage(Message m)  
     {  
      if(m.what == 0)  
      {  
       System.out.println("Handle message");  
      
      }       
         
     }  
    }

}
